﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Objects.DataClasses;
using System.Data.Objects;
using DA = System.ComponentModel.DataAnnotations;

namespace WebUI.Models
{
    public partial class SiteDb : ObjectContext
    {
        public static readonly Guid APPID = new Guid("273f9136-97a7-4926-a740-18a89538c817");
        public long LastIDSeed = 0;
        public long LastIDFix = 0;
        public static DateTime ZeroTime = new DateTime(2012, 1, 1);
        object newidlocker = new object();
        public string NewID()
        {
            long ts = (long)(DateTime.Now - ZeroTime).TotalSeconds;
            if (ts > LastIDSeed)
            {
                lock (newidlocker)
                {
                    if (ts > LastIDSeed)
                    {
                        LastIDFix = 0;
                        LastIDSeed = ts;
                    }
                }
            }
            return (LastIDSeed * 1000 + (LastIDFix++)).ToString().TrimEnd('0');

        }

        public ObjectSet<T> GetModelByName<T>(string name) where T : class
        {
            var mo = this.GetType().GetProperty(name).GetValue(this, null);
            var m = mo as ObjectSet<T>;
            return m;
        }

        public IQueryable<aspnet_Users> Doctors
        {
            get
            {
                return aspnet_Roles.Where(v => v.RoleName == "Doctor").First().aspnet_Users.AsQueryable();
            }
        }

        public IQueryable<EnumDefine> EnumDefine_NoSys
        {
            get
            {

                return EnumDefines.Where(v => !v.IsSys);
            }
        }

        public IQueryable<Hospital> Hospitals_Fixed
        {
            get {
                return Hospitals.Where(v => v.HospitalID != "10001");
            }
        }
        public void DeleteAll(object[] objs)
        {
            foreach (var obj in objs)
            {
                this.DeleteObject(obj);
            }
        }
    }
    public partial class QizSubject : EntityObject
    {
        public bool NeedReport { get; set; }
        public bool CalcNeedReport(aspnet_Users u)
        {
            var needreport = false;
            if (StatusID == "100")
            {
                var lastsubmit = QizSessions.Where(v => v.UserID == u.UserId).OrderByDescending(v => v.Timemark).FirstOrDefault();
                var needsubmitdate = lastsubmit == null ? CreateTime : lastsubmit.Timemark.AddDays(ActionInterval);

                needreport = (needsubmitdate <= DateTime.Today);
            }
            NeedReport = needreport;
            return needreport;
        }
    }
    public partial class aspnet_Users : EntityObject
    {
        public Recuper QueryCurrentRecuperForDoctor(aspnet_Users Doctor) {
            return this.UserAllRecupers.Where(v => v.DepartmentID == Doctor.DepartmentID).OrderByDescending(v => v.EnHospDay).First();
        }


    }

}